/**
 * @program: HelpLuojia
 * @description: 失物招领service实现类
 * @author: PXM
 * @create: 2022-11-21 19:08
 **/
package com.whu.helpluojia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whu.helpluojia.dao.AdminMessageDao;
import com.whu.helpluojia.dao.FindDao;
import com.whu.helpluojia.dao.LostAndFoundDao;
import com.whu.helpluojia.dao.MessageDao;
import com.whu.helpluojia.domain.*;
import com.whu.helpluojia.service.LostAndFoundService;
import com.whu.helpluojia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class LostAndFoundServiceImpl extends ServiceImpl<LostAndFoundDao, LostAndFound> implements LostAndFoundService {

    @Autowired
    LostAndFoundDao lostAndFoundDao;

    @Autowired
    FindDao findDao;

    @Autowired
    UserService userService;

    @Autowired
    MessageDao messageDao;

    @Autowired
    AdminMessageDao adminMessageDao;


//    @Autowired
//    FindService findService;

    /**
     * 判断是否在寻物启事中 => 返回有可能的寻物启事
     * @param lostAndFound
     * @return
     */
    @Override
    public List<Find> insert(LostAndFound lostAndFound) {
        List<Find> res = new ArrayList<>();
        //1.判断
        //Find[] finds = findService.selectAll();
        List<Find> findList = findDao.selectList(null);
        Find[] finds = new Find[findList.size()];
        //迭代器遍历List集合
        Iterator<Find> iterator = findList.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            Find next = iterator.next();
            finds[i++] = next;//先赋值再做加法运算
        }
        //主要逻辑判断
        for(Find find : finds){
            if(find.getLonMin() <= lostAndFound.getLon() && lostAndFound.getLon() <= find.getLonMax()
            && find.getLatMin() <= lostAndFound.getLat() && lostAndFound.getLat() <= find.getLatMax()){
                res.add(find);
            }
        }

        LambdaQueryWrapper<Message> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Message::getId);
        queryWrapper.last("limit 1");
        Message messageMax=messageDao.selectOne(queryWrapper);
        Long maxId=messageMax.getId();

        LambdaQueryWrapper<LostAndFound> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.orderByDesc(LostAndFound::getId);
        queryWrapper1.last("limit 1");
        LostAndFound lostAndFound1=lostAndFoundDao.selectOne(queryWrapper1);
        Long maxIdLos=lostAndFound1.getId()+1;

        //2.为res中的每个find对应的userId存一条系统消息
        for(Find find:res){
            maxId=maxId+1;
            Message message=new Message();
            message.setContent("您之前发布的寻物启事有相近的匹配结果，请您查看");
            message.setSenderId(1L);
            message.setReceiverId(find.getUserId());
            message.setTime(new Date());
            messageDao.insert(message);

            AdminMessage adminMassage=new AdminMessage();
            adminMassage.setMessageId(maxId);
            adminMassage.setCategory("失物招领");
            adminMassage.setPublishId(lostAndFound1.getId());
            adminMessageDao.insert(adminMassage);
        }


        //3.将捡东西的数据写入数据库
        lostAndFoundDao.insert(lostAndFound);

        return res;
    }

    /**
     * 修改状态为已解决
     * @param lostAndFound
     */
    @Override
    public void status(LostAndFound lostAndFound) {
        lostAndFound.setStatus(1);
        lostAndFoundDao.updateById(lostAndFound);
    }

    @Override
    public void solute(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", 1);
        lostAndFoundDao.update(null, updateWrapper);
    }

    @Override
    public MyPublish getLafDetail(Long id, Long userId) {

        LostAndFound lostAndFound=lostAndFoundDao.selectById(id);

        User user=userService.selectUser(userId);

        MyPublish myPublish=new MyPublish();

        myPublish=copyData(myPublish,lostAndFound,user);

        return myPublish;
    }

    @Override
    public void delete() {

    }

    @Override
    public LostAndFound[] selectAll() {

//        QueryWrapper<LostAndFound> lostAndFoundQueryWrapper = new QueryWrapper<>();
//        lostAndFoundQueryWrapper.isNotNull("id");

        List<LostAndFound> list = lostAndFoundDao.selectList(null);
        LostAndFound[] array = new LostAndFound[list.size()];
        for(int i = 0;i < list.size();i++){
            array[i] = list.get(i);
        }
        return array;
    }

    @Override
    public List<MyPublish> getMyDetail(Long userId) {

        LambdaQueryWrapper<LostAndFound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LostAndFound::getUserId,userId);
        List<LostAndFound> lostAndFoundList=lostAndFoundDao.selectList(lambdaQueryWrapper);

        User user=userService.selectUser(userId);

        List<MyPublish> myPublishes=new ArrayList<>();

        for(LostAndFound lostAndFound : lostAndFoundList){
            MyPublish myPublish=new MyPublish();
            myPublish=copyData(myPublish,lostAndFound,user);
            myPublishes.add(myPublish);
        }

        return myPublishes;
    }

    public MyPublish copyData(MyPublish myPublish,LostAndFound find,User user){
        myPublish.setId(find.getId().longValue());
        myPublish.setCategory("失物招领");
        myPublish.setContent(find.getDescription());
        myPublish.setSex(user.getGender());
        myPublish.setStatus(find.getStatus());
        myPublish.setSenderName(user.getName());
        myPublish.setTime(find.getTime());
        myPublish.setType(find.getType());
        return myPublish;

    }
}
